<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class SimplelistingController extends JController
{

    function __construct()
    {
        parent::__construct();
        $document =& JFactory::getDocument();
    }

    function display()
    {
        if ( ! JREQUEST::getCMD( 'view')) {
            JREQUEST::setVar('view', 'listings');
        }
        parent::display();
    }

    function disFav() //list favourite
    {
        $viewName   = JRequest::getVar( 'view', 'favourite' );
        $viewLayout = JRequest::getVar( 'layout', 'default' );
        $view = & $this->getView('favourite', 'html');
        $view = & $this->getView($viewName);
        $view->setLayout($viewLayout);
        $view->FavShow();
    }

    function show()
    {
        $cids = JRequest::getVar('cid', null, 'DEFAULT', 'array' );
        if($cids === null) {
            JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }
        $simplelistingId = (int)$cids[0];

        $viewName   = JRequest::getVar( 'view', 'listing' );
        $viewLayout = JRequest::getVar( 'layout', 'default' );
        $view = & $this->getView('listing', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('listing');
        if (!$model){
            JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $simplelisting = $model->getSimplelistingLists($simplelistingId);

        $view->setLayout($viewLayout);
        $view->displayShow($simplelistingId);
    }

    function showAuthor()
    {
        $cids = JRequest::getVar('cid', null, 'DEFAULT', 'array' );
        if($cids === null) {
            JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }
        $authorId = (int)$cids[0];

        $viewName   = JRequest::getVar( 'view', 'author' );
        $viewLayout = JRequest::getVar( 'layout', 'default' );
        $view = & $this->getView('author', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('author');
        if (!$model){
            JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $author = $model->getAuthors($authorId);

        $view->setLayout($viewLayout);
        $view->displayShowAuthor($authorId);
    }


    function edit()
    {
        $cids = JRequest::getVar('cid', null, 'DEFAULT', 'array' );
        if($cids === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }
        $simplelistingId = (int)$cids[0];

        $viewName   = JRequest::getVar( 'view', 'manage' );
        $viewLayout = JRequest::getVar( 'layout', 'listingedit' );
        $view = & $this->getView('manage', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('manage');
        if (!$model){
                JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $simplelisting = $model->getSimplelistingLists($simplelistingId);

        $view->setLayout($viewLayout);
        $view->displayEdit($simplelistingId);
    }

    function add()
    {
        $viewName   = JRequest::getVar( 'view', 'manage' );
        $viewLayout = JRequest::getVar( 'layout', 'listingedit' );
        $view = & $this->getView('manage', 'html');
        $view = & $this->getView($viewName);

        $model = & $this->getModel('manage');
        if (!$model){
                JError::raiseError(500, JText::_('MODEL_MISSING_MSG'));
        }
        $view->setModel($model, true);
        $simplelisting = $model->getNewSimplelisting();

        $view->setLayout($viewLayout);
        $view->displayAdd();
    }

    function save()
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $app = JFactory::getApplication();
        jimport('joomla.filter.output');
        $simplelisting = array(
            'id'             => JRequest::getVar( 'id', '', 'POST' ),
            'name'           => JRequest::getVar( 'name', '', 'POST' ),
            'description'    => JRequest::getVar( 'description', '', 'POST', 'string', JREQUEST_ALLOWHTML),
            'address'        => JRequest::getVar( 'address', '', 'POST' ),
            'countries_id'   => JRequest::getInt( 'countries_id', 0 , 'POST' ),
            'price'          => JRequest::getFloat( 'price', 0.0, 'POST' ),
            'type_id'        => JRequest::getInt( 'type_id', 0 , 'POST' ),
            'category_id'    => JRequest::getInt( 'category_id', 0 , 'POST' ),
            'extra_id'       => JRequest::getInt( 'extra_id', 0 , 'POST' ),
            'layout_id'      => JRequest::getInt( 'layout_id', 0 , 'POST' ),
            'published'      => JRequest::getInt( 'published', 0 , 'POST' )
        );

        if ($create_by = JRequest::getInt( 'create_by', 0 , 'POST' )){
            $simplelisting['create_by'] = $create_by;
        }
        if ($date_created = JRequest::getVar( 'date_created', '' , 'POST' )){
            $simplelisting['date_created'] = $date_created;
        }

        if ($modify_by = JRequest::getInt( 'modify_by', 0 , 'POST' )){
            $simplelisting['modify_by'] = $modify_by;
        }
        if ($date_modified = JRequest::getVar( 'date_modified', '' , 'POST' )){
            $simplelisting['date_modified'] = $date_modified;
        }

        if ($facilities = JRequest::getVar('facilities', array(0), 'POST', 'array' )){
            if ( !empty($facilities) && is_array($facilities) ) {

            sort( $facilities );
            $facilities = implode(',' , $facilities );
        }
        else {
                $facilities = '';
             }
             $simplelisting['facilities'] = $facilities;
        }

        $model = & $this->getModel('manage');
        $model->saveSimplelisting($simplelisting);

        $msg = $app->enqueueMessage(JText::_('SAVE_ITEM_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&view=manage&layout=manage'), $msg);
    }


    function remove()
    {
        $app = JFactory::getApplication();
        $arrayIDs = JRequest::getVar('cid', null, 'manage', 'array' );
        if($arrayIDs === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = & $this->getModel('manage');
        $model->deleteSimplelisting($arrayIDs);

        $msg = $app->enqueueMessage(JText::_('ITEM_DELETED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&view=manage&layout=manage'), $msg);
    }

    function deleteFav()
    {
        $app = JFactory::getApplication();
        $FIDs = JRequest::getVar('fid', array(0), 'favourite', 'array' );
        if($FIDs === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = & $this->getModel('favourite');
        $model->deleteFav($FIDs);

        $msg = $app->enqueueMessage(JText::_('FAV_DELETED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&view=favourite'), $msg);
    }

   function upload_main()
    {
       JRequest::checkToken() or jexit('Invalid Token');
        $app = JFactory::getApplication();
        $file = JRequest::getVar( 'upload_file', null, 'files', 'array' );
        $id = JRequest::getVar('id', '', 'post', 'id');

        $model = $this->getModel('manage');
        $model->uploadFile($file, $id);

        $msg = $app->enqueueMessage(JText::_('IMAGE_UPLOADED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=edit&cid[]='.$id), $msg);
    }

    function upload_second()
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $app = JFactory::getApplication();
        $file = JRequest::getVar( 'upload_file', array(), 'files', 'array' );
        $id = JRequest::getVar('id', '', 'post', 'id');

        if($file === null){ //Make sure the cid parameter was in the request
			JError::raiseError(500, JText::_('CID_MISSING_MSG'));
		}

        $model = $this->getModel('manage');
        $model->uploadFiles($file, $id);

        $msg = $app->enqueueMessage(JText::_('IMAGE_UPLOADED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=edit&cid[]='.$id), $msg);
    }

    function delete_main()
    {
        $app = JFactory::getApplication();
        $id = JRequest::getVar('id', '', 'detail');

        if($id === null){
                JError::raiseError(500, JText::_('CID_MISSING_MSG'));
        }

        $model = & $this->getModel('manage');
        $model->deleteFile($id);

        $msg = $app->enqueueMessage(JText::_('IMAGE_DELETED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=edit&cid[]='.$id), $msg);
    }

    function delete_sec()
    {
        $app = JFactory::getApplication();
        $id = JRequest::getVar('id', '', 'detail');
        $image = JRequest::getVar('image', '', 'detail');

        $model = & $this->getModel('manage');
        $model->deleteFiles($id,$image);

        $msg = $app->enqueueMessage(JText::_('IMAGE_DELETED_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=edit&cid[]='.$id), $msg);
    }

    function favourite()
    {
        $app = JFactory::getApplication();
        $id = JRequest::getVar('id', '', 'detail');
	$user =& JFactory::getUser();
        $user_id = $user->id;

        if(empty($user_id)){
            $redirectTo = JRoute::_('index.php?option='.JRequest::getVar('option'));
            $this->setRedirect($redirectTo.'&task=show&cid[]='.$id, (JText::_('LOGIN_MSG')));

        }else{

        $model = & $this->getModel('favourite');
        $model->setFav($id, $user_id);

        $msg = $app->enqueueMessage(JText::_('ADDED_FAVOURITE_MSG'), 'cwinformation');
        $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=show&cid[]='.$id), $msg);
        }
    }

    function sendmail()
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $app = JFactory::getApplication();
        jimport('joomla.mail.helper');
        jimport('joomla.utilities.utility');
        $config =& JFactory::getConfig();
        $app = JFactory::getApplication();
        $db =& JFactory::getDBO();
        $id = JRequest::getVar('id', '', 'detail');
        $email = JMailHelper::cleanAddress( JRequest::getVar('email', '') );
        $name = JRequest::getVar('name', '');
        $subject = JRequest::getVar('subject', '') . ' [' .$config->getValue('fromname', '') . ']';
        $message = JRequest::getVar('e_message', '');

        if ( empty($name) ) {
            $msg = $app->enqueueMessage(JText::_('SPECIFY_NAME_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=show&cid[]='.$id), $msg);

        } if ( empty($message) ) {
            $msg = $app->enqueueMessage(JText::_('SPECIFY_MESSAGE_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=show&cid[]='.$id), $msg);

            } if ( !JMailHelper::isEmailAddress($email) ) {
            $msg = $app->enqueueMessage(JText::_('INVALID_EMAIL_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=show&cid[]='.$id), $msg);

            } else {

                $create_by = JRequest::getInt('create_by', 0);
                $sql = 'SELECT `email` FROM `#__users` WHERE `id`=' . intval($create_by);
                $db->setQuery($sql);
                $reciptient = $db->loadResult();

            if (empty($reciptient)) {
                $reciptient = $config->getValue('mailfrom', '');
            }

                $sendOk = JUtility::sendMail($email, $name, $reciptient ,$subject , $message, false);

                if( $sendOk ) {
                $msg = $app->enqueueMessage(JText::_('MESSAGE_SENT_MSG'), 'cwinformation');
                $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=show&cid[]='.$id), $msg);

                        JRequest::setVar('name' , '');
                        JRequest::setVar('subject', '');
                        JRequest::setVar('email', '');
                        JRequest::setVar('e_message', '');
                } else {
                    $msg = JError::raiseWarning( 500, JText::_( 'SENDMAIL_ERROR_MSG'));
                    $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=show&cid[]='.$id), $msg);
                }
        }
        $this->display();
    }

    function sendmailAuthor()
    {
        JRequest::checkToken() or jexit('Invalid Token');
        $app = JFactory::getApplication();
        jimport('joomla.mail.helper');
        jimport('joomla.utilities.utility');
        $config =& JFactory::getConfig();
        $app = JFactory::getApplication();
        $db =& JFactory::getDBO();
        $id = JRequest::getVar('id', '', 'detail');
        $email = JMailHelper::cleanAddress( JRequest::getVar('email', '') );
        $name = JRequest::getVar('name', '');
        $subject = JRequest::getVar('subject', '') . ' [' .$config->getValue('fromname', '') . ']';
        $message = JRequest::getVar('e_message', '');

        if ( empty($name) ) {
            $msg = $app->enqueueMessage(JText::_('SPECIFY_NAME_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=showAuthor&cid[]='.$id), $msg);

        } if ( empty($message) ) {
            $msg = $app->enqueueMessage(JText::_('SPECIFY_MESSAGE_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=showAuthor&cid[]='.$id), $msg);

            } if ( !JMailHelper::isEmailAddress($email) ) {
            $msg = $app->enqueueMessage(JText::_('INVALID_EMAIL_MSG'), 'cwinformation');
            $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=showAuthor&cid[]='.$id), $msg);

            } else {

                $create_by = JRequest::getInt('create_by', 0);
                $sql = 'SELECT `email` FROM `#__users` WHERE `id`=' . intval($id);
                $db->setQuery($sql);
                $reciptient = $db->loadResult();

            if (empty($reciptient)) {
                $reciptient = $config->getValue('mailfrom', '');
            }

                $sendOk = JUtility::sendMail($email, $name, $reciptient ,$subject , $message, false);

                if( $sendOk ) {
                $msg = $app->enqueueMessage(JText::_('MESSAGE_SENT_MSG'), 'cwinformation');
                $app->redirect(JRoute::_('index.php?option='.JRequest::getVar('option').'&task=showAuthor&cid[]='.$id), $msg);

                        JRequest::setVar('name' , '');
                        JRequest::setVar('subject', '');
                        JRequest::setVar('email', '');
                        JRequest::setVar('e_message', '');
                } else {
                    $msg = JError::raiseWarning( 500, JText::_( 'SENDMAIL_ERROR_MSG'));
                    $app->redirect(JRoute::_('index.php?option=com_simplelisting&task=showAuthor&cid[]='.$id), $msg);
                }
        }
        $this->display();
    }


    function filteritems()
    {
        jimport('joomla.utilities.utility');
        $config =& JFactory::getConfig();
        $app = JFactory::getApplication();
        $db =& JFactory::getDBO();
        $fcategory_id = JRequest::getVar('fcategory_id', '');
        $ftype_id = JRequest::getVar('ftype_id', '');
        $fcountry_id = JRequest::getVar('fcountry_id', '');
        $fextra_id = JRequest::getVar('fextra_id', '');
        $flayout_id = JRequest::getVar('flayout_id', '');
        $forder_id = JRequest::getVar('forder_id', '');

        JRequest::setVar('fcategory_id' , '');
        JRequest::setVar('ftype_id' , '');
        JRequest::setVar('fcountry_id' , '');
        JRequest::setVar('fextra_id' , '');
        JRequest::setVar('flayout_id' , '');
        JRequest::setVar('forder_id' , '');

        $app->redirect(JRoute::_('index.php?option=com_simplelisting&view=listings&catid='.$fcategory_id.'&typeid='.$ftype_id.'&countryid='.$fcountry_id.'&extraid='.$fextra_id.'&layoutid='.$flayout_id.'&orderby='.$forder_id));


        $this->display();


    }
}
